package com.ph.elec.gwt.register.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.ph.elec.gwt.register.client.OptionLists;
import com.ph.elec.gwt.register.client.RegisterService;
import com.ph.elec.gwt.register.client.RegisterServiceAsync;
import com.ph.elec.gwt.util.UIUtils;
import com.ph.elec.gwt.util.UIUtils.StdDecoratorPanel;

public class AddressPanel extends StdDecoratorPanel {
	
	public final ListBox lstAdrRegion = new ListBox();
	public final ListBox lstAdrProvince = new ListBox();
	public final ListBox lstAdrCity = new ListBox();
	public final ListBox lstAdrBarangay = new ListBox();

	public TextBox txtStreetAddr = new TextBox();
	public TextBox txtZipCode = new TextBox();

	public ListBox lstDiocese = new ListBox();
	public TextBox txtParish = new TextBox();

	public AddressPanel() {
		HorizontalPanel hzpRow = new HorizontalPanel();
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstAdrRegion, OptionLists.REGION), 
						"250px"), 
				"Region", 
				true));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstAdrProvince, OptionLists.SELECT_FIRST), 
						"163px"), 
				"Province", 
				true));
		vtpRoot.add(hzpRow);

		hzpRow = new HorizontalPanel();
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstAdrCity, OptionLists.SELECT_FIRST), 
						"206px"), 
				"Municipality/City", 
				true));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstAdrBarangay, OptionLists.SELECT_FIRST), 
						"207px"), 
				"Barangay", 
				false));
		vtpRoot.add(hzpRow);

		hzpRow = new HorizontalPanel();
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtStreetAddr, "330px", 80), 
				"Street Address", 
				false));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtZipCode, "70px", 5), 
				"Zip Code", 
				false));
		vtpRoot.add(hzpRow);
		
		hzpRow = new HorizontalPanel();
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstDiocese, OptionLists.DIOCESE), 
						"200px"), 
				"Diocese", 
				false));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtParish, "208px", 50), 
				"Parish", 
				false));
		vtpRoot.add(hzpRow);

		add(vtpRoot);
		setWidth("500px");
		
		initializeContents();
		initializeHandlers();
	}
	
	private void initializeContents() {
		lstAdrCity.setEnabled(false);
		lstAdrProvince.setEnabled(false);
		lstAdrBarangay.setEnabled(false);
	}

	protected void initializeHandlers() {
		lstAdrRegion.addChangeHandler(new ChangeHandler() {
			final RegisterServiceAsync registerSv = GWT.create(RegisterService.class);

			public void onChange(ChangeEvent event) {
				clearProvinces();
				clearCities();
				clearBarangays();
				if (lstAdrRegion.getSelectedIndex() == 0) {
					return;
				}
				AsyncCallback<List<String[]>> callback = new AsyncCallback<List<String[]>>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(List<String[]> result) {
						for (String[] item : result) {
							lstAdrProvince.addItem(item[1], item[0]);
						}
						lstAdrProvince.setEnabled(true);
					}
				};
				registerSv.GetProvinces(OptionLists.REGION_CODES.get(lstAdrRegion.getSelectedIndex() - 1), callback);
			}
		});

		lstAdrProvince.addChangeHandler(new ChangeHandler() {
			final RegisterServiceAsync registerSv = GWT.create(RegisterService.class);

			public void onChange(ChangeEvent event) {
				clearCities();
				clearBarangays();
				if (lstAdrProvince.getSelectedIndex() == 0) {
					return;
				}
				AsyncCallback<List<String[]>> callback = new AsyncCallback<List<String[]>>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(List<String[]> result) {
						for (String[] item : result) {
							lstAdrCity.addItem(item[1], item[0]);
						}
						lstAdrCity.setEnabled(true);
					}
				};
				registerSv.GetCities(lstAdrProvince.getValue(lstAdrProvince.getSelectedIndex()), callback);
			}
		});

		lstAdrCity.addChangeHandler(new ChangeHandler() {
			final RegisterServiceAsync registerSv = GWT.create(RegisterService.class);

			public void onChange(ChangeEvent event) {
				clearBarangays();
				if (lstAdrCity.getSelectedIndex() == 0) {
					return;
				}
				AsyncCallback<List<String[]>> callback = new AsyncCallback<List<String[]>>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(List<String[]> result) {
						for (String[] item : result) {
							lstAdrBarangay.addItem(item[1], item[0]);
						}
						lstAdrBarangay.setEnabled(true);
					}
				};
				registerSv.GetBarangays(lstAdrCity.getValue(lstAdrCity.getSelectedIndex()), callback);
			}
		});
	}
	
	final public void clearProvinces() {
		lstAdrProvince.setSelectedIndex(0);
		while (lstAdrProvince.getItemCount() >= 2) {
			lstAdrProvince.removeItem(1);
		}
		lstAdrProvince.setEnabled(false);
	}

	final public void clearCities() {
		lstAdrCity.setSelectedIndex(0);
		while (lstAdrCity.getItemCount() >= 2) {
			lstAdrCity.removeItem(1);
		}
		lstAdrCity.setEnabled(false);
	}

	final public void clearBarangays() {
		lstAdrBarangay.setSelectedIndex(0);
		while (lstAdrBarangay.getItemCount() >= 2) {
			lstAdrBarangay.removeItem(1);
		}
		lstAdrBarangay.setEnabled(false);
	}
	
	public boolean validate() {
		boolean invalid = false;
		vtpErrorMessage.clear();
		if (lstAdrRegion.getSelectedIndex() == 0) {
			addErrorMessage("Region is required.");
			invalid = true;
		}
		if (lstAdrProvince.getSelectedIndex() == 0) {
			addErrorMessage("Province is required.");
			invalid = true;
		}
		if (lstAdrCity.getSelectedIndex() == 0) {
			addErrorMessage("Municipality/City is required.");
			invalid = true;
		}
		if (invalid) {
			return false;
		}
		return true;
	}
}
